﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class liuyan_addqian : System.Web.UI.Page
{
// 2 请求参数成员变量列表
﻿    public string request_biaoti = "";
﻿    public string request_neirong = "";
﻿    public string request_liuyanshijian = "";
﻿    public string request_usersid = "";
﻿    public string request_huifu = "";


    //初始化当前时间
    public string current_shijian = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").Trim();
    //初始化当前日期
    public string current__riqi = DateTime.Now.ToString("yyyy-MM-dd").Trim();

    //默认执行页面载入代码
    protected void Page_Load(object sender, EventArgs e)
    {
//首先登陆判断
﻿
        //登录判断
        if (Session["usersidx"] != null && Session["usersidx"].ToString() != "")
        {
            //登录状态不为空
        }
        else
        {
            //登录状态为空
            Response.Write("<script>javascript:alert('对不起,您没有这个权限或未登录!');history.back();</script>");
            Response.End();
        }


        if (!IsPostBack)
        {
//3 初始化参数列表
﻿            if (IsExistControl("liuyanshijian"))
            {
                GetControl("liuyanshijian").Text = current_shijian;
            }
﻿
            if (Session["usersidx"] != null && Session["usersidx"].ToString() != "")
            {
                string fatherIdColumnValue = "";
                fatherIdColumnValue = Session["usersidx"].ToString();
                if (IsExistControl("usersid"))
                {
                    addselectbyId("users", "id", "loginname", "usersid", fatherIdColumnValue);
                }
            }
            else
            {
                Response.Write("<script>javascript:alert('对不起,您没有这个权限或未登录!');history.back();</script>");
                Response.End();
            }



            ////Response.Write("第一次,非回发");
        }
        else
        {
            ////Response.Write("第一次以后,回发");
        }
    }

    protected void Add_Button_Click(object sender, EventArgs e)
    {

﻿        //重复判断
        //if (IsExistControl("biaoti"))
        //{
            //ischongfu("select id from liuyan where  biaoti='" + biaoti.Text.ToString().Trim() + "'");
        //}


        string sql = "";
        string sqlProperty = "";
        string sqlValue = "";
        string sqlTail = ")";

        sqlProperty = "insert into liuyan(";
        sqlValue = ") values(";


//4 组织sql语句的属性与值
﻿
            if (IsExistControl("biaoti"))
            {
                IsEmptyControl("biaoti");
    
                sqlProperty = sqlProperty + "biaoti,";
                sqlValue = sqlValue + "'" + GetControl("biaoti").Text.ToString().Trim() + "',";
            }
﻿
            if (IsExistControl("neirong"))
            {
                IsEmptyControl("neirong");
    
                sqlProperty = sqlProperty + "neirong,";
                sqlValue = sqlValue + "'" + GetControl("neirong").Text.ToString().Trim() + "',";
            }
﻿
            if (IsExistControl("liuyanshijian"))
            {
                IsEmptyControl("liuyanshijian");
    
                sqlProperty = sqlProperty + "liuyanshijian,";
                sqlValue = sqlValue + "'" + GetControl("liuyanshijian").Text.ToString().Trim() + "',";
            }
﻿
            if (IsExistControl("usersid"))
            {
                IsEmptyControl("usersid");
    
                sqlProperty = sqlProperty + "usersid,";
                sqlValue = sqlValue + "'" + GetControl("usersid").Text.ToString().Trim() + "',";
            }
﻿
            if (IsExistControl("huifu"))
            {
                IsEmptyControl("huifu");
    
                sqlProperty = sqlProperty + "huifu,";
                sqlValue = sqlValue + "'" + GetControl("huifu").Text.ToString().Trim() + "',";
            }



        sqlProperty = sqlProperty.Substring(0, sqlProperty.Length - 1);
        sqlValue = sqlValue.Substring(0, sqlValue.Length - 1);
        sql = sqlProperty + sqlValue + sqlTail;
        int result;
        result = new Class1().hsgexucute(sql);
        if (result == 1)
        {
            Response.Write("<script>javascript:alert('操作完成，继续操作！');history.back();</script>");
        }
        else
        {
            Response.Write("<script>javascript:alert('系统错误，请检查数据库设置问题');history.back();</script>");
        }
    }

    //选择父类信息
    private void addselect(string FatherTable, string FatherIdColumnName, string FatherSecondColumnName, string DropDownListControlId)
    {
        //控件不存在则返回
        if (this.FindControl(DropDownListControlId) == null)
        {
            //Response.Write("控件未找到!!!   " + DropDownListControlId);
            return;
        }

        string sql;
        sql = "select " + FatherIdColumnName + "," + FatherSecondColumnName + " from " + FatherTable + " order by id desc";
        DataSet dsresult = new DataSet();
        dsresult = new Class1().hsggetdata(sql);
        if (dsresult != null)
        {
            if (dsresult.Tables[0].Rows.Count > 0)
            {

                //ListItem it0 = new ListItem();
                //it0.Value = "";
                //it0.Text = "全部";
                //((DropDownList)this.FindControl(DropDownListControlId)).Items.Add(it0);
                for (int i = 0; i < dsresult.Tables[0].Rows.Count; i++)
                {
                    ListItem it = new ListItem();
                    it.Value = dsresult.Tables[0].Rows[i][0].ToString().Trim();
                    it.Text = dsresult.Tables[0].Rows[i][1].ToString().Trim();
                    ((DropDownList)this.FindControl(DropDownListControlId)).Items.Add(it);
                }

            }
        }
    }

    //通过当前信息id作为父类id选择父类
    private void addselectbyId(string FatherTable, string FatherIdColumnName, string FatherSecondColumnName, string DropDownListControlId, string FatherIdColumnValue)
    {
        //控件不存在则返回
        if (this.FindControl(DropDownListControlId) == null)
        {
            //Response.Write("控件未找到!!!   " + DropDownListControlId);
            return;
        }

        string sql;
        sql = "select " + FatherIdColumnName + "," + FatherSecondColumnName + " from " + FatherTable + " where " + FatherIdColumnName + "='" + FatherIdColumnValue + "'" + " order by id desc";
        DataSet dsresult = new DataSet();
        dsresult = new Class1().hsggetdata(sql);
        if (dsresult != null)
        {
            if (dsresult.Tables[0].Rows.Count > 0)
            {

                //ListItem it0 = new ListItem();
                //it0.Value = "";
                //it0.Text = "全部";
                //((DropDownList)this.FindControl(DropDownListControlId)).Items.Add(it0);
                for (int i = 0; i < dsresult.Tables[0].Rows.Count; i++)
                {

                    ListItem it = new ListItem();
                    it.Value = dsresult.Tables[0].Rows[i][0].ToString().Trim();
                    it.Text = dsresult.Tables[0].Rows[i][1].ToString().Trim();
                    ((DropDownList)this.FindControl(DropDownListControlId)).Items.Add(it);
                }

            }
        }
    }

    //获取父类信息
    private string getFatherSecondColumnValue(string FatherTable, string FatherSecondColumnName, string FatherIdColumnVaule)
    {
        string FatherSecondColumnValue = "";
        string sql;
        sql = "select " + FatherSecondColumnName + " from " + FatherTable + " where 1=1 and id='" + FatherIdColumnVaule + "' order by id desc";

        DataSet dsresult = new DataSet();
        dsresult = new Class1().hsggetdata(sql);
        if (dsresult != null)
        {
            if (dsresult.Tables[0].Rows.Count > 0)
            {
                FatherSecondColumnValue = dsresult.Tables[0].Rows[0][0].ToString().Trim();
            }
        }

        return FatherSecondColumnValue;
    }

    public void ischongfu(string sql)//是否重复判断
    {
        DataSet result = new DataSet();
        result = new Class1().hsggetdata(sql);
        if (result != null)
        {
            if (result.Tables[0].Rows.Count > 0)
            {
                Response.Write("<script>javascript:alert('提示,信息已存在,不要重复添加');history.back();</script>");
                Response.End();
            }
        }
    }

    public void IsEmptyControl(string ControlId)//判断控件内容是否为空
    {
        if (!IsExistControl(ControlId) || new Class1().IsEmpty(GetControl(ControlId).Text.ToString().Trim()))
        {
            Response.Write("<script>javascript:alert('输入不能为空,请重新输入!');history.back();</script>");
            Response.End();
        }
    }


    public void IsHandset(string ControlId)//判断控件内容是否手机号
    {
        IsEmptyControl(ControlId);
        if (!new Class1().IsHandset(GetControl(ControlId).Text.ToString().Trim()))
        {
            Response.Write("<script>javascript:alert('输入格式不正确,请重新输入手机号格式!');history.back();</script>");
            Response.End();
        }
    }

    public void IsEmail(string ControlId)//判断控件内容是否邮箱
    {
        IsEmptyControl(ControlId);
        if (!new Class1().IsEmail(GetControl(ControlId).Text.ToString().Trim()))
        {
            Response.Write("<script>javascript:alert('输入格式不正确,请重新输入邮箱格式!');history.back();</script>");
            Response.End();
        }
    }

    public void IsIDcard(string ControlId)//判断控件内容是否身份证号
    {
        IsEmptyControl(ControlId);
        if (!new Class1().IsIDcard(GetControl(ControlId).Text.ToString().Trim()))
        {
            Response.Write("<script>javascript:alert('输入格式不正确,请重新输入身份证格式!');history.back();</script>");
            Response.End();
        }
    }

    public bool IsExistControl(string DropDownListControlId)  //判断控件是否存在
    {
        if (this.FindControl(DropDownListControlId) != null)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public ITextControl GetControl(string ControlId)  //获取ITextControl控件对象
    {
        if (this.FindControl(ControlId) != null)
        {
            return (ITextControl)this.FindControl(ControlId);
        }
        else
        {
            return null;
        }
    }

    public TextBox GetTextBoxControl(string TextBoxControlId)  //获取TextBox控件对象
    {
        if (this.FindControl(TextBoxControlId) != null)
        {
            return (TextBox)this.FindControl(TextBoxControlId);
        }
        else
        {
            return null;
        }
    }

    public DropDownList GetDropDownListControl(string DropDownListControlId)  //获取DropDownList控件对象
    {
        if (this.FindControl(DropDownListControlId) != null)
        {
            return (DropDownList)this.FindControl(DropDownListControlId);
        }
        else
        {
            return null;
        }
    }

}

